<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Link4A&nbsp;在线驱动管理系统 | 重置密码</title>
<%@include file="/include/style.jsp" %>
<%@include file="/include/script.jsp" %>
<link href="/css/page/login.css?v=<%=Constants.VERSION %>" rel="stylesheet" type="text/css" />
</head>
<body>
<c:if test="${errMessage == null}">
	<legend>
		<h3>请重新设置您的密码</h3>
	</legend>
	<div style="font-size: 15px;">
		<p>
			　用户名：
			${username}
		</p>
		<p>
			　新密码：
			<input type="password" id="pwd" onkeyup="enterToReset(event);" style="margin-bottom: 0;" />
			<span class="text-danger" id="pwdError"></span>
		</p>
		
		<p>
			确认密码：
			<input type="password" id="chkPwd" onkeyup="enterToReset(event);" style="margin-bottom: 0" />
			<span class="text-danger" id="chkPwdError"></span>
		</p>
		<div style="margin-left: 80px;">
			<button id="btnResetPwd" class="btn btn-success" onclick="resetPwd();">重置密码</button>
			&nbsp;&nbsp;<a href="/">返回登录页</a>
		</div>
	</div>
</c:if>

<c:if test="${errMessage != null}">
	<h3>${errMessage}</h3>
	<div><a href="/">返回登录页</a></div>
</c:if>
<script type="text/javascript">
var applyCode = '${applyCode}';
var $pwd = $("#pwd");
var $chkPwd = $("#chkPwd");
var $btnResetPwd = $("#btnResetPwd");
var $pwdError = $("#pwdError");
var $chkPwdError = $("#chkPwdError");
var pwdValue = "";
var confirmPwdValue = "";

function resetPwd() {
	$btnResetPwd.attr('disabled', true);
	$btnResetPwd.addClass("cursorDefault");
	if(validForm()) {
		$.post("/login/resetPassword", {password: $pwd.val(), applyCode: applyCode}, function(response) {
			if (response.success) {
				Message.success("提示", "修改成功！", function() {
					location.href='/';
				});
			} else {
				Message.error("提示", "修改失败，请重新申请修改密码！", function() {
					location.href='/';
					$("#btnResetPwd").attr('disabled', false);
					$btnResetPwd.removeClass("cursorDefault");
				});
			}
		});
	}
}

function enterToReset(event) {
	$btnResetPwd.attr('disabled', true);
	$btnResetPwd.addClass("cursorDefault");
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	
	if(validPassword()){
		$("#btnResetPwd").attr('disabled', false);
		$btnResetPwd.removeClass("cursorDefault");
		if (keyCode == 13)
			resetPwd();
	} else {
		return false;
	}
}

 function validPassword() {
	var password = $pwd.val();
	if(password.length < 6){
		$pwdError.html("密码长度不能小于6位");
		return false;
	}
	if(password.length > 24){
		$pwdError.html("密码长度不能大于24位");
		return false;
	}
	$pwdError.html("");
	if(!validConfirmPwd()) {
		$chkPwdError.html("两次输入的密码不一致");
		return false;
	}
	$chkPwdError.html("");
	return true;
}

function validConfirmPwd() {
 	  	if ($pwd.val() && $chkPwd.val() && $pwd.val() != $chkPwd.val()) {
		return false;
	}
 	  	return true;
}

function validForm() {
	if(!$pwd.val()) {
		$pwdError.html("请输入新密码");
		$pwd.focus();
		return false;
	}
	$pwdError.html("");
	
	if(!$chkPwd.val()) {
		$chkPwdError.html("请输入确认密码");
		$chkPwd.focus();
		return false;
	}		
	$chkPwdError.html("");		
	return true;
}
</script>
</body>
</html>